<?php

class RssSpiderUtils
{
    public static function GetResource( $url )
    {
        $curlHandler = curl_init($url);
        curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, true);

        $resource = curl_exec($curlHandler);
        curl_close($curlHandler);

        return $resource;
    }

    public static function GenerateImagePathById( $postImageId )
    {
        return sprintf(RssPostImage::PATH_PATTERN, $postImageId);
    }

    /**
     * Convert BR tags to nl
     *
     * @param string The string to convert
     * @return string The converted string
     */
    public static function br2nl( $string )
    {
        return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
    }

    /**
     * Returns string shortened to MAX_LENGTH.
     *
     * @param string $content
     */
    public static function GetShortContent( $content )
    {
        $MAX_LENGTH = 500;
        $contentLength = strlen( $content );
        if ( $contentLength <= $MAX_LENGTH )
        {
            return $content;
        }

        $res = "";
        for ( $charIndex = 0; $charIndex < $contentLength; ++$charIndex )
        {
            if ( ( $content[$charIndex] == " " ) && ( $charIndex >= $MAX_LENGTH ) )
            {
                break;
            }
             else
            {
                $res .= $content[$charIndex];
            }
        }

        return $res;
    }
}

?>